package com.harry.martin_skill_master.util;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Martin-harry
 * @date 2021/8/10
 * @address
 * @Desc ActivityControler
 */
public class ActivityCollector {
    public static List<Activity> activities = new ArrayList<>();

    /**
     * 向List中添加一个活动
     *
     * @param activity 活动
     */
    public static void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 从List中移除活动
     *
     * @param activity 活动
     */
    public static void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    /**
     * 将List中存储的活动全部销毁掉
     */
    public static void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    /**
     * 获取Activity栈中的栈顶的Activity
     *
     * 需要注意的是，栈是先进后出，所以最上面的Activity是集合中的最后一个
     */
    public static Activity getTopActivity() {
        if (activities.isEmpty()) {
            //Activity栈为空
            return null;
        } else {
            //不为空时
            return activities.get(activities.size() - 1);
        }
    }

    public static int getActivity() {
        return activities.size();
    }
}
